package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.*;
import com.example.common.util.R;
import com.example.buildtable.service.RoleMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "角色授权")
@RequestMapping(value = "/roleMenu/")
public class RoleMenuController {
    @Autowired
    private RoleMenuService roleMenuService;

    @Operation(summary = "查询所有角色授权数据")
    @GetMapping(value = "/getAllRoleMenu")
    public R getAllRoleMenu() {
        LambdaJoinQueryWrapper<RoleMenu> wrapper = new LambdaJoinQueryWrapper<>(RoleMenu.class);
        wrapper.innerJoin(Role.class, Role::getRoleId, RoleMenu::getRoleId)
                .innerJoin(ColumnMenu.class, ColumnMenu::getColumnMenuId, RoleMenu::getColumnMenuId)
                .innerJoin(OnelevelMenu.class, OnelevelMenu::getOnelevelMenuId, ColumnMenu::getOnelevelMenuId)
                .innerJoin(TwolevelMenu.class, TwolevelMenu::getTwolevelMenuId, ColumnMenu::getTwolevelMenuId);
        List<RoleMenu> roleMenuList = roleMenuService.list(wrapper);
        return R.ok("查询所有角色授权数据成功").data("roleMenuList", roleMenuList);
    }
    
    @Operation(summary = "增加角色授权")
    @PostMapping(value = "/addRoleMenu")
    public R addRoleMenu(RoleMenu roleMenu) {
        boolean flag = roleMenuService.save(roleMenu);
        if (flag) {
            return R.ok("角色授权增加成功");
        } else {
            return R.ok("角色授权增加失败");
        }
    }

    @Operation(summary = "删除角色授权")
    @GetMapping(value = "/deleteRoleMenu/{roleMenuId}")
    public R deleteRoleMenu(@PathVariable("roleMenuId") Integer roleMenuId) {
        boolean flag = roleMenuService.removeById(roleMenuId);
        if (flag) {
            return R.ok("角色授权删除成功");
        } else {
            return R.ok("角色授权删除失败");
        }
    }

    @Operation(summary = "按照角色授权查看")
    @GetMapping(value = "/getRoleMenuById/{roleMenuId}")
    public R getRoleMenuById(@PathVariable("roleMenuId") Integer roleMenuId) {
        LambdaJoinQueryWrapper<RoleMenu> wrapper = new LambdaJoinQueryWrapper<>(RoleMenu.class);
        wrapper.innerJoin(Role.class, Role::getRoleId, RoleMenu::getRoleId)
                .innerJoin(ColumnMenu.class, ColumnMenu::getColumnMenuId, RoleMenu::getColumnMenuId);
        wrapper.eq(RoleMenu::getRoleMenuId, roleMenuId);
        RoleMenu roleMenu = roleMenuService.getOne(wrapper);
        if (roleMenu != null) {
            return R.ok("按照角色授权查看成功").data("roleMenu", roleMenu);
        } else {
            return R.error("按照角色授权查看失败");
        }
    }

    @Operation(summary = "修改角色授权")
    @PostMapping(value = "/updateRoleMenu")
    public R updateRoleMenu(RoleMenu roleMenu) {
        boolean flag = roleMenuService.updateById(roleMenu);
        if (flag) {
            return R.ok("角色授权修改成功");
        } else {
            return R.ok("角色授权修改失败");
        }
    }
}